package com.shuzi.MediaShare.gui.search;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.download.DownloadManager;

/**
 * 基础activity框架抽象类
 * 
 * @author lung 下午9:09:42 2013
 */
public abstract class SearchBaseFrameAtivity extends Activity
{

	/** 加载等待视图 */
	protected RelativeLayout loadingView = null;

	/** 错误时显示的视图 */
	protected RelativeLayout errorView = null;

	/** 中间视图 */
	private RelativeLayout centerViewLayout = null;

	// /** 标题栏 */
	// protected TitileBarLayout titileBarLayout = null;

	/** 重试按钮 */
	private Button tryAgainButton = null;

	/** 遇到错误后是否显示错误视图 */
	protected boolean isShowErrorView = true;

	/** 是否已经销毁 */
	private boolean isDestory = false;

	/** 返回按钮 */
	private ImageView backImageView;

	/** 搜索按钮 */
	protected Button searchButton;

	protected EditText searchEditText;

	@Override
	protected void onDestroy()
	{
		isDestory = true;
		super.onDestroy();
	}

	public void setShowErrorView(boolean isShowErrorView)
	{
		this.isShowErrorView = isShowErrorView;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		initView();
	}

	/**
	 * 初始化视图
	 */
	private void initView()
	{
		setContentView(R.layout.search_main);
		loadingView = (RelativeLayout) findViewById(R.id.loadingView);
		centerViewLayout = (RelativeLayout) findViewById(R.id.centerView);
		errorView = (RelativeLayout) findViewById(R.id.errorView);
		searchEditText = (EditText) findViewById(R.id.searchEditText);
		// titileBarLayout = (TitileBarLayout) findViewById(R.id.main_titlebar);
		backImageView = (ImageView) findViewById(R.id.back_button);
		searchButton = (Button) findViewById(R.id.search_button);
		centerViewLayout.addView(setCenterView(),
				new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
						LayoutParams.FILL_PARENT));
		backImageView.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();

			}
		});
		// titileBarLayout.setOnTitleClick(new OnTitleBarClick()
		// {
		//
		// @Override
		// public void onClick(View view)
		// {
		// if (view.getId() != R.id.choice_button)
		// {
		// finish();
		// } else
		// {
		// showTypeChoice();
		// }
		//
		// }
		//
		// });
		tryAgainButton = (Button) findViewById(R.id.tryAgain);
		tryAgainButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (DownloadManager.getInstace().isNetWorksAvailable)
				{
					if (errorView != null)
					{
						errorView.setVisibility(View.GONE);
					}
					loadingView.setVisibility(View.VISIBLE);
					tryAgain();
				} else
				{
					Toast.makeText(SearchBaseFrameAtivity.this, "无网络，请设置网络",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	// /**
	// * 设置标题栏是否显示
	// *
	// * @param visible
	// */
	// public void setTitleBarVisible(boolean visible)
	// {
	// if (titileBarLayout != null)
	// {
	// if (visible)
	// {
	// titileBarLayout.setVisibility(View.VISIBLE);
	// } else
	// {
	// titileBarLayout.setVisibility(View.GONE);
	// }
	// }
	// }

	/**
	 * 
	 * 加载数据
	 * 
	 * @param params
	 */
	public void loadData(Integer... params)
	{

		if (!DownloadManager.getInstace().isNetWorksAvailable)
		{
			loadingView.setVisibility(View.GONE);
			errorView.setVisibility(View.VISIBLE);
			Toast.makeText(SearchBaseFrameAtivity.this, "无网络，请设置网络",
					Toast.LENGTH_SHORT).show();
			return;
		}

		new AsyncTask<Integer, Void, Boolean>()
		{

			Integer[] paIntegers;

			@Override
			protected Boolean doInBackground(Integer... params)
			{
				this.paIntegers = params;
				return initData(params);
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				loadingView.setVisibility(View.GONE);
				// if (!result && isShowErrorView)
				// {
				// errorView.setVisibility(View.VISIBLE);
				// }
				if (!isDestory)
				{
					refreshView(result, paIntegers);
				}

			}

		}.execute(params);
	}

	/**
	 * 
	 * 耗时操作获取数据
	 * 
	 * @param params
	 * @return
	 */
	protected abstract boolean initData(Integer... params);

	/**
	 * 获取数据完成后刷新视图
	 * 
	 * @param params
	 * @param result
	 *            是否成功
	 * @return
	 */
	protected abstract void refreshView(boolean isSuccessful, Integer... params);

	/**
	 * 重试
	 */
	protected abstract void tryAgain();

	/**
	 * 子类中间视图
	 * 
	 * @return
	 */
	protected abstract View setCenterView();

	/**
	 * 设置筛选的视图
	 */
	protected void showTypeChoice()
	{

	}

	/**
	 * 设置全屏
	 */
	protected void setFullScreen()
	{
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	/**
	 * 退出全屏
	 */
	protected void quitFullScreen()
	{
		final WindowManager.LayoutParams attrs = getWindow().getAttributes();
		attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setAttributes(attrs);
		getWindow()
				.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}

}
